package cn.hdu.goods.cart.servlet;

import cn.hdu.goods.book.domain.Book;
import cn.hdu.goods.cart.domain.CartItem;
import cn.hdu.goods.cart.domain.Quantity;
import cn.hdu.goods.cart.service.CartItemService;
import cn.hdu.goods.user.domain.User;
import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/CartItemServlet")
public class CartItemServlet extends BaseServlet {

    private CartItemService cartItemService = new CartItemService();

    public String myCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("sessionUser");
        String uid = user.getUid();

        List<CartItem> cartItemList = cartItemService.myCart(uid);

        req.setAttribute("cartItemList", cartItemList);
        return "f:/jsps/cart/list.jsp";
    }

    public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map map = req.getParameterMap();
        CartItem cartItem = CommonUtils.toBean(map, CartItem.class);
        Book book = CommonUtils.toBean(map, Book.class);
        User user = (User) req.getSession().getAttribute("sessionUser");
        cartItem.setBook(book);
        cartItem.setUser(user);

        cartItemService.add(cartItem);

        return myCart(req, resp);
    }

    public String batchDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cartItemIds = req.getParameter("cartItemIds");
        cartItemService.batchDelete(cartItemIds);
        return myCart(req, resp);
    }

    public String updateQuantity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cartItemId = req.getParameter("cartItemId");
        int quantity = Integer.parseInt(req.getParameter("quantity"));
        CartItem cartItem = cartItemService.updateQuantity(cartItemId, quantity);
        Quantity q = new Quantity(String.valueOf(cartItem.getQuantity()),
                String.valueOf(cartItem.getSubtotal()));
        String json = new Gson().toJson(q);
        resp.getWriter().print(json);
        return null;
    }

    public String loadCartItems(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cartItemIds = req.getParameter("cartItemIds");
        double total = Double.parseDouble(req.getParameter("total"));

        List<CartItem> cartItemList = cartItemService.loadCartItems(cartItemIds);
        req.setAttribute("cartItemList", cartItemList);
        req.setAttribute("total", total);
        req.setAttribute("cartItemIds",cartItemIds);
        return "f:/jsps/cart/showitem.jsp";
    }
}